clc
close all
clear all
sample_size = 2000;
coefficient = 8/6;
MEAN = 0;
SIQR = 1;

if (MEAN == 1)
  %%PRKS
  prks95_delayslot = LinkMeanDelay('prks95_delayslot_type2.txt',sample_size);
  prks95_delayslot = sort(prks95_delayslot);
  prks90_delayslot = LinkMeanDelay('prks90_delayslot_type2.txt',sample_size);
  prks90_delayslot = sort(prks90_delayslot);
  prks80_delayslot = LinkMeanDelay('prks80_delayslot_type2.txt',sample_size);
  prks80_delayslot = sort(prks80_delayslot);
  prks70_delayslot = LinkMeanDelay('prks70_delayslot_type2.txt',sample_size);
  prks70_delayslot = sort(prks70_delayslot);
  prks60_delayslot = LinkMeanDelay('prks60_delayslot_type2.txt',sample_size);
  prks60_delayslot = sort(prks60_delayslot);
  prks50_delayslot = LinkMeanDelay('prks50_delayslot_type2.txt',sample_size);
  prks50_delayslot = sort(prks50_delayslot);
  prks40_delayslot = LinkMeanDelay('prks40_delayslot_type2.txt',sample_size);
  prks40_delayslot = sort(prks40_delayslot);
  prks30_delayslot = LinkMeanDelay('prks30_delayslot_type2.txt',sample_size);
  prks30_delayslot = sort(prks30_delayslot);
  %%RID
  rid95_delayslot = LinkMeanDelay('rid95_delayslot_type2.txt',sample_size);
  rid95_delayslot = sort(rid95_delayslot);
  rid90_delayslot = LinkMeanDelay('rid90_delayslot_type2.txt',sample_size);
  rid90_delayslot = sort(rid90_delayslot);
  rid80_delayslot = LinkMeanDelay('rid80_delayslot_type2.txt',sample_size);
  rid80_delayslot = sort(rid80_delayslot);
  rid70_delayslot = LinkMeanDelay('rid70_delayslot_type2.txt',sample_size);
  rid70_delayslot = sort(rid70_delayslot);
  %%B-MAC
  csma_delayslot = LinkMeanDelay('csma_delayslot_type2.txt',sample_size);
  csma_delayslot = sort(csma_delayslot*coefficient);
  %%S-MAC
  rtscts_delayslot = LinkMeanDelay('rtscts_delayslot_type2.txt',sample_size);
  rtscts_delayslot = sort(rtscts_delayslot*coefficient);
  %%CMAC
  cmac95_delayslot = LinkMeanDelay('cmac95_delayslot_type2.txt',sample_size);
  cmac95_delayslot = sort(cmac95_delayslot*coefficient);
  cmac90_delayslot = LinkMeanDelay('cmac90_delayslot_type2.txt',sample_size);
  cmac90_delayslot = sort(cmac90_delayslot*coefficient);
  cmac80_delayslot = LinkMeanDelay('cmac80_delayslot_type2.txt',sample_size);
  cmac80_delayslot = sort(cmac80_delayslot*coefficient);
  cmac70_delayslot = LinkMeanDelay('cmac70_delayslot_type2.txt',sample_size);
  cmac70_delayslot = sort(cmac70_delayslot*coefficient);
  %%SCREAM
  scream_delayslot = LinkMeanDelay('scream_delayslot_type2.txt',sample_size);
  scream_delayslot = sort(scream_delayslot);


  CSMA_mean_diff = [];
  for i=1:length(csma_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == csma_delayslot(i,1)/coefficient
            CSMA_mean_diff = [CSMA_mean_diff;  csma_delayslot(i,1)/coefficient csma_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

RTSCTS_mean_diff = [];
for i=1:length(rtscts_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == rtscts_delayslot(i,1)/coefficient
            RTSCTS_mean_diff = [RTSCTS_mean_diff;  rtscts_delayslot(i,1)/coefficient rtscts_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

SCREAM_mean_diff = [];
for i=1:length(scream_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == scream_delayslot(i,1)
            SCREAM_mean_diff = [SCREAM_mean_diff;  scream_delayslot(i,1) scream_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

RID_mean_diff70 = [];
for i=1:length(rid70_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == rid70_delayslot(i,1)
            RID_mean_diff70 = [RID_mean_diff70;  rid70_delayslot(i,1) rid70_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

RID_mean_diff80 = [];
for i=1:length(rid80_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == rid80_delayslot(i,1)
            RID_mean_diff80 = [RID_mean_diff80;  rid80_delayslot(i,1) rid80_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

RID_mean_diff90 = [];
for i=1:length(rid90_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == rid90_delayslot(i,1)
            RID_mean_diff90 = [RID_mean_diff90;  rid90_delayslot(i,1) rid90_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

RID_mean_diff95 = [];
for i=1:length(rid95_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == rid95_delayslot(i,1)
            RID_mean_diff95 = [RID_mean_diff95;  rid95_delayslot(i,1) rid95_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

CMAC_mean_diff70 = [];
for i=1:length(cmac70_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == cmac70_delayslot(i,1)/coefficient
            CMAC_mean_diff70 = [CMAC_mean_diff70;  cmac70_delayslot(i,1)/coefficient cmac70_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

CMAC_mean_diff80 = [];
for i=1:length(cmac80_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == cmac80_delayslot(i,1)/coefficient
            CMAC_mean_diff80 = [CMAC_mean_diff80;  cmac80_delayslot(i,1)/coefficient cmac80_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

CMAC_mean_diff90 = [];
for i=1:length(cmac90_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == cmac90_delayslot(i,1)/coefficient
            CMAC_mean_diff90 = [CMAC_mean_diff90;  cmac90_delayslot(i,1)/coefficient cmac90_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

CMAC_mean_diff95 = [];
for i=1:length(cmac95_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == cmac95_delayslot(i,1)/coefficient
            CMAC_mean_diff95 = [CMAC_mean_diff95;  cmac95_delayslot(i,1)/coefficient cmac95_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

PRKS_mean_diff95 = [];
for i=1:length(prks95_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == prks95_delayslot(i,1)
            PRKS_mean_diff95 = [PRKS_mean_diff95;  prks95_delayslot(i,1) prks95_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

PRKS_mean_diff80 = [];
for i=1:length(prks80_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == prks80_delayslot(i,1)
            PRKS_mean_diff80 = [PRKS_mean_diff80;  prks80_delayslot(i,1) prks80_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

PRKS_mean_diff70 = [];
for i=1:length(prks70_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == prks70_delayslot(i,1)
            PRKS_mean_diff70 = [PRKS_mean_diff70;  prks70_delayslot(i,1) prks70_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end
PRKS_mean_diff60 = [];
for i=1:length(prks60_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == prks60_delayslot(i,1)
            PRKS_mean_diff60 = [PRKS_mean_diff60;  prks60_delayslot(i,1) prks60_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end
PRKS_mean_diff50 = [];
for i=1:length(prks50_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == prks50_delayslot(i,1)
            PRKS_mean_diff50 = [PRKS_mean_diff50;  prks50_delayslot(i,1) prks50_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end
PRKS_mean_diff40 = [];
for i=1:length(prks40_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == prks40_delayslot(i,1)
            PRKS_mean_diff40 = [PRKS_mean_diff40;  prks40_delayslot(i,1) prks40_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end
PRKS_mean_diff30 = [];
for i=1:length(prks30_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == prks30_delayslot(i,1)
            PRKS_mean_diff30 = [PRKS_mean_diff30;  prks30_delayslot(i,1) prks30_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

%%===============================================

results = {0};
results{1} = PRKS_mean_diff95(:,2);
%results{2} = PRKS_mean_diff90(:,2);
results{2} = PRKS_mean_diff80(:,2);
results{3} = PRKS_mean_diff70(:,2);
results{4} = PRKS_mean_diff60(:,2);
results{5} = PRKS_mean_diff50(:,2);
results{6} = PRKS_mean_diff40(:,2);
results{7} = PRKS_mean_diff30(:,2);

results{8} = RID_mean_diff95(:,2);
results{9} = RID_mean_diff90(:,2);
results{10} = RID_mean_diff80(:,2);
results{11} = RID_mean_diff70(:,2);


results{12} = SCREAM_mean_diff(:,2);

group = zeros(0);
dataDisp = zeros(0);
for i = 1 : size(results, 2)
    group = [group ; ones(size(results{i})) + i - 1];
    dataDisp = [dataDisp ; results{i}];
end
figure1 = figure;

%boxplot(dataDisp, group, 'notch', 'on','labels',{'70%','80%',  '90%','95%'});hold on; set(findobj(gca,'Type','text'),'FontSize',40)
handler=boxplot(dataDisp, group, 'notch', 'on');hold on;
%set(gca, 'YTickLabel', sprintf('%d%%|', 70:5:100))
set(gca,'XTickMode','auto','XTickLabelMode','auto','XTick',[1 2 3 4 5 6 7 8 9 10 11 12],'XTickLabel',{'PRKS95','PRKS80','PRKS70','PRKS60','PRKS50','PRKS40','PRKS30','RIDB95','RIDB90','RIDB80','RIDB70','SCREAM'});
%set(figure1, 'ResizeFcn', {@resizeCallback, gca});
ylabel('per link mean difference');
maximize ();
end

if (SIQR == 1)
  %%PRKS
  prks95_delayslot = LinkSiqrDelay('prks95_delayslot_type2.txt',sample_size);
  prks95_delayslot = sort(prks95_delayslot);
  prks90_delayslot = LinkSiqrDelay('prks90_delayslot_type2.txt',sample_size);
  prks90_delayslot = sort(prks90_delayslot);
  prks80_delayslot = LinkSiqrDelay('prks80_delayslot_type2.txt',sample_size);
  prks80_delayslot = sort(prks80_delayslot);
  prks70_delayslot = LinkSiqrDelay('prks70_delayslot_type2.txt',sample_size);
  prks70_delayslot = sort(prks70_delayslot);
  prks60_delayslot = LinkSiqrDelay('prks60_delayslot_type2.txt',sample_size);
  prks60_delayslot = sort(prks60_delayslot);
  prks50_delayslot = LinkSiqrDelay('prks50_delayslot_type2.txt',sample_size);
  prks50_delayslot = sort(prks50_delayslot);
  prks40_delayslot = LinkSiqrDelay('prks40_delayslot_type2.txt',sample_size);
  prks40_delayslot = sort(prks40_delayslot);
  prks30_delayslot = LinkSiqrDelay('prks30_delayslot_type2.txt',sample_size);
  prks30_delayslot = sort(prks30_delayslot);
  %%RID
  rid95_delayslot = LinkSiqrDelay('rid95_delayslot_type2.txt',sample_size);
  rid95_delayslot = sort(rid95_delayslot);
  rid90_delayslot = LinkSiqrDelay('rid90_delayslot_type2.txt',sample_size);
  rid90_delayslot = sort(rid90_delayslot);
  rid80_delayslot = LinkSiqrDelay('rid80_delayslot_type2.txt',sample_size);
  rid80_delayslot = sort(rid80_delayslot);
  rid70_delayslot = LinkSiqrDelay('rid70_delayslot_type2.txt',sample_size);
  rid70_delayslot = sort(rid70_delayslot);
  %%B-MAC
  csma_delayslot = LinkSiqrDelay('csma_delayslot_type2.txt',sample_size);
  csma_delayslot = sort(csma_delayslot*coefficient);
  %%S-MAC
  rtscts_delayslot = LinkSiqrDelay('rtscts_delayslot_type2.txt',sample_size);
  rtscts_delayslot = sort(rtscts_delayslot*coefficient);
  %%CMAC
  cmac95_delayslot = LinkSiqrDelay('cmac95_delayslot_type2.txt',sample_size);
  cmac95_delayslot = sort(cmac95_delayslot*coefficient);
  cmac90_delayslot = LinkSiqrDelay('cmac90_delayslot_type2.txt',sample_size);
  cmac90_delayslot = sort(cmac90_delayslot*coefficient);
  cmac80_delayslot = LinkSiqrDelay('cmac80_delayslot_type2.txt',sample_size);
  cmac80_delayslot = sort(cmac80_delayslot*coefficient);
  cmac70_delayslot = LinkSiqrDelay('cmac70_delayslot_type2.txt',sample_size);
  cmac70_delayslot = sort(cmac70_delayslot*coefficient);
  %%SCREAM
  scream_delayslot = LinkSiqrDelay('scream_delayslot_type2.txt',sample_size);
  scream_delayslot = sort(scream_delayslot);


  CSMA_siqr_diff = [];
  for i=1:length(csma_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == csma_delayslot(i,1)/coefficient
            CSMA_siqr_diff = [CSMA_siqr_diff;  csma_delayslot(i,1)/coefficient csma_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

RTSCTS_siqr_diff = [];
for i=1:length(rtscts_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == rtscts_delayslot(i,1)/coefficient
            RTSCTS_siqr_diff = [RTSCTS_siqr_diff;  rtscts_delayslot(i,1)/coefficient rtscts_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

SCREAM_siqr_diff = [];
for i=1:length(scream_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == scream_delayslot(i,1)
            SCREAM_siqr_diff = [SCREAM_siqr_diff;  scream_delayslot(i,1) scream_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

RID_siqr_diff70 = [];
for i=1:length(rid70_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == rid70_delayslot(i,1)
            RID_siqr_diff70 = [RID_siqr_diff70;  rid70_delayslot(i,1) rid70_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

RID_siqr_diff80 = [];
for i=1:length(rid80_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == rid80_delayslot(i,1)
            RID_siqr_diff80 = [RID_siqr_diff80;  rid80_delayslot(i,1) rid80_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

RID_siqr_diff90 = [];
for i=1:length(rid90_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == rid90_delayslot(i,1)
            RID_siqr_diff90 = [RID_siqr_diff90;  rid90_delayslot(i,1) rid90_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

RID_siqr_diff95 = [];
for i=1:length(rid95_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == rid95_delayslot(i,1)
            RID_siqr_diff95 = [RID_siqr_diff95;  rid95_delayslot(i,1) rid95_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

CMAC_siqr_diff70 = [];
for i=1:length(cmac70_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == cmac70_delayslot(i,1)/coefficient
            CMAC_siqr_diff70 = [CMAC_siqr_diff70;  cmac70_delayslot(i,1)/coefficient cmac70_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

CMAC_siqr_diff80 = [];
for i=1:length(cmac80_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == cmac80_delayslot(i,1)/coefficient
            CMAC_siqr_diff80 = [CMAC_siqr_diff80;  cmac80_delayslot(i,1)/coefficient cmac80_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

CMAC_siqr_diff90 = [];
for i=1:length(cmac90_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == cmac90_delayslot(i,1)/coefficient
            CMAC_siqr_diff90 = [CMAC_siqr_diff90;  cmac90_delayslot(i,1)/coefficient cmac90_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

CMAC_siqr_diff95 = [];
for i=1:length(cmac95_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == cmac95_delayslot(i,1)/coefficient
            CMAC_siqr_diff95 = [CMAC_siqr_diff95;  cmac95_delayslot(i,1)/coefficient cmac95_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

PRKS_siqr_diff95 = [];
for i=1:length(prks95_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == prks95_delayslot(i,1)
            PRKS_siqr_diff95 = [PRKS_siqr_diff95;  prks95_delayslot(i,1) prks95_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

PRKS_siqr_diff80 = [];
for i=1:length(prks80_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == prks80_delayslot(i,1)
            PRKS_siqr_diff80 = [PRKS_siqr_diff80;  prks80_delayslot(i,1) prks80_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

PRKS_siqr_diff70 = [];
for i=1:length(prks70_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == prks70_delayslot(i,1)
            PRKS_siqr_diff70 = [PRKS_siqr_diff70;  prks70_delayslot(i,1) prks70_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end
PRKS_siqr_diff60 = [];
for i=1:length(prks60_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == prks60_delayslot(i,1)
            PRKS_siqr_diff60 = [PRKS_siqr_diff60;  prks60_delayslot(i,1) prks60_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end
PRKS_siqr_diff50 = [];
for i=1:length(prks50_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == prks50_delayslot(i,1)
            PRKS_siqr_diff50 = [PRKS_siqr_diff50;  prks50_delayslot(i,1) prks50_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end
PRKS_siqr_diff40 = [];
for i=1:length(prks40_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == prks40_delayslot(i,1)
            PRKS_siqr_diff40 = [PRKS_siqr_diff40;  prks40_delayslot(i,1) prks40_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end
PRKS_siqr_diff30 = [];
for i=1:length(prks30_delayslot)
    for j = 1:length (prks90_delayslot)
        if prks90_delayslot(j,1) == prks30_delayslot(i,1)
            PRKS_siqr_diff30 = [PRKS_siqr_diff30;  prks30_delayslot(i,1) prks30_delayslot(i,2)-prks90_delayslot(j,2)];
        end
    end
end

%%===============================================

results = {0};
results{1} = PRKS_siqr_diff95(:,2);
%results{2} = PRKS_siqr_diff90(:,2);
results{2} = PRKS_siqr_diff80(:,2);
results{3} = PRKS_siqr_diff70(:,2);
results{4} = PRKS_siqr_diff60(:,2);
results{5} = PRKS_siqr_diff50(:,2);
results{6} = PRKS_siqr_diff40(:,2);
results{7} = PRKS_siqr_diff30(:,2);

results{8} = RID_siqr_diff95(:,2);
results{9} = RID_siqr_diff90(:,2);
results{10} = RID_siqr_diff80(:,2);
results{11} = RID_siqr_diff70(:,2);


results{12} = SCREAM_siqr_diff(:,2);

group = zeros(0);
dataDisp = zeros(0);
for i = 1 : size(results, 2)
    group = [group ; ones(size(results{i})) + i - 1];
    dataDisp = [dataDisp ; results{i}];
end
figure1 = figure;

%boxplot(dataDisp, group, 'notch', 'on','labels',{'70%','80%',  '90%','95%'});hold on; set(findobj(gca,'Type','text'),'FontSize',40)
handler=boxplot(dataDisp, group, 'notch', 'on');hold on;
%set(gca, 'YTickLabel', sprintf('%d%%|', 70:5:100))
set(gca,'XTickMode','auto','XTickLabelMode','auto','XTick',[1 2 3 4 5 6 7 8 9 10 11 12],'XTickLabel',{'PRKS95','PRKS80','PRKS70','PRKS60','PRKS50','PRKS40','PRKS30','RIDB95','RIDB90','RIDB80','RIDB70','SCREAM'});
%set(figure1, 'ResizeFcn', {@resizeCallback, gca});
ylabel('per link siqr difference');
maximize ();
end
